home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 551-575 / disk_562 / intuisup / editor / source.lzh / project.c < prev    next >
C/C++ Source or Header  |  1991-10-25  |  15KB  |  552 lines

  1.         /*************************************
  2.          *                                   *
  3.          *            Editor v1.0            *
  4.          *   by Torsten Jürgeleit in 07/91   *
  5.          *                                   *
  6.          *            Project part           *
  7.          *                                   *
  8.          *************************************/
  9.  
  10.     /* Includes */
  11.  
  12. #include "includes.h"
  13. #include "defines.h"
  14. #include "imports.h"
  15. #include "protos.h"
  16.  
  17.     /* Defines */
  18.  
  19. #define MOUSE_BUTTON_SELECT        (1 << 0)
  20. #define MOUSE_BUTTON_MENU        (1 << 1)
  21. #define MOUSE_BUTTON_RUBBER_BAND    (1 << 2)
  22.  
  23. #define PROJECT_TITLE_REFRESH_DELAY    500000   /* µs */
  24.  
  25.     /* Perform project action */
  26.  
  27.    SHORT
  28. perform_project_action(VOID)
  29. {
  30.    struct MsgPort         *up = pwin->UserPort;
  31.    struct IntuiMessage    *msg;
  32.    struct Template        *tp;
  33.    struct Box             *box = ¤t_box;
  34.    ULONG seconds, micros, last_seconds, last_micros;
  35.    SHORT mouse_x, mouse_y, snap_x, snap_y, status = EDITOR_STATUS_NORMAL;
  36.  
  37.    while (msg = IGetMsg(up)) {
  38.       mouse_x = msg->MouseX;
  39.       mouse_y = msg->MouseY;
  40.       snap_x  = (mouse_x + (snap_offset >> 1)) / snap_offset * snap_offset;
  41.       snap_y  = (mouse_y + (snap_offset >> 1)) / snap_offset * snap_offset;
  42.       switch (msg->Class) {
  43.      case MOUSEBUTTONS :
  44.         switch (msg->Code) {
  45.                  case SELECTDOWN :
  46.           clear_template_info();
  47.           switch (editor_mode) {
  48.              case EDITOR_MODE_CREATE :
  49.             box->bo_X1    = box->bo_X2 = snap_x;
  50.             box->bo_Y1    = box->bo_Y2 = snap_y;
  51.             mouse_button |= MOUSE_BUTTON_RUBBER_BAND;
  52.             draw_box(pwin, box);
  53.             break;
  54.  
  55.              case EDITOR_MODE_MODIFY :
  56.             if (tp = get_template_by_pos(mouse_x, mouse_y)) {
  57.                draw_box(pwin, &tp->tp_Box);
  58.                last_snap_x   = snap_x;
  59.                last_snap_y   = snap_y;
  60.                modify_mode   = get_modify_mode(mouse_x, mouse_y);
  61.                mouse_button |= MOUSE_BUTTON_RUBBER_BAND;
  62.                print_template_info();
  63.             }
  64.             break;
  65.  
  66.              case EDITOR_MODE_CLONE :
  67.             if (tp = get_template_by_pos(mouse_x, mouse_y)) {
  68.                draw_box(pwin, &tp->tp_Box);
  69.                last_snap_x   = snap_x;
  70.                last_snap_y   = snap_y;
  71.                mouse_button |= MOUSE_BUTTON_RUBBER_BAND;
  72.                print_template_info();
  73.             }
  74.             break;
  75.  
  76.              case EDITOR_MODE_DELETE :
  77.              case EDITOR_MODE_EDIT :
  78.             if (get_template_by_pos(mouse_x, mouse_y)) {
  79.                mouse_button |= MOUSE_BUTTON_RUBBER_BAND;
  80.                print_template_info();
  81.             }
  82.             break;
  83.           }
  84.           mouse_button |= MOUSE_BUTTON_SELECT;
  85.           if (editor_mode != EDITOR_MODE_USE) {
  86.              print_project_window_title();
  87.           }
  88.           break;
  89.  
  90.            case SELECTUP :
  91.           tp = selected_template;
  92.           switch (editor_mode) {
  93.              case EDITOR_MODE_CREATE :
  94.             if (mouse_button & MOUSE_BUTTON_RUBBER_BAND) {
  95.                box->bo_X2 = snap_x;
  96.                box->bo_Y2 = snap_y;
  97.                fix_template_bounds();
  98.                end_rubber_banding();
  99.             }
  100.             break;
  101.  
  102.              case EDITOR_MODE_CLONE :
  103.             clear_template_info();
  104.             if (mouse_x < 0 || mouse_x > pwin->Width ||
  105.                mouse_y < 0 || mouse_y > pwin->Height) {
  106.                DisplayBeep((LONG)NULL);
  107.             } else {
  108.                box->bo_X1 += snap_x - last_snap_x;
  109.                box->bo_Y1 += snap_y - last_snap_y;
  110.                box->bo_X2 += snap_x - last_snap_x;
  111.                box->bo_Y2 += snap_y - last_snap_y;
  112.                fix_template_bounds();
  113.                end_rubber_banding();
  114.             }
  115.             tp->tp_Flags &= ~TEMPLATE_FLAG_MODIFIED;
  116.             break;
  117.  
  118.              case EDITOR_MODE_MODIFY :
  119.             if (mouse_button & MOUSE_BUTTON_RUBBER_BAND) {
  120.                clear_template_info();
  121.                if (tp->tp_Flags & TEMPLATE_FLAG_MODIFIED) {
  122.                   tp->tp_Flags &= ~TEMPLATE_FLAG_MODIFIED;
  123.                   if (mouse_x < 0 || mouse_x > pwin->Width ||
  124.                     mouse_y < 0 || mouse_y > pwin->Height) {
  125.                  DisplayBeep((LONG)NULL);
  126.                   } else {
  127.                  if (modify_mode == MODIFY_MODE_MOVE) {
  128.                     box->bo_X1 += snap_x - last_snap_x;
  129.                     box->bo_Y1 += snap_y - last_snap_y;
  130.                     box->bo_X2 += snap_x - last_snap_x;
  131.                     box->bo_Y2 += snap_y - last_snap_y;
  132.                  } else {
  133.                     box->bo_X2 += snap_x - last_snap_x;
  134.                     box->bo_Y2 += snap_y - last_snap_y;
  135.                  }
  136.                  fix_template_bounds();
  137.                  end_rubber_banding();
  138.                   }
  139.                }
  140.             }
  141.             break;
  142.  
  143.              case EDITOR_MODE_DELETE :
  144.             if (mouse_button & MOUSE_BUTTON_RUBBER_BAND) {
  145.                clear_template_info();
  146.                delete_template(selected_template);
  147.             }
  148.             break;
  149.  
  150.              case EDITOR_MODE_EDIT :
  151.             if (mouse_button & MOUSE_BUTTON_RUBBER_BAND) {
  152.                info_template  = NULL;
  153.                info_displayed = FALSE;
  154.                status         = EDITOR_STATUS_EDIT;
  155.             }
  156.             break;
  157.           }
  158.           mouse_button &= ~(MOUSE_BUTTON_SELECT |
  159.                           MOUSE_BUTTON_RUBBER_BAND);
  160.           if (editor_mode != EDITOR_MODE_USE) {
  161.              print_project_window_title();
  162.              ActivateWindow(ewin);
  163.           }
  164.           break;
  165.  
  166.            case MENUDOWN :
  167.           mouse_button |= MOUSE_BUTTON_MENU;
  168.           break;
  169.  
  170.            case MENUUP :
  171.           mouse_button &= ~MOUSE_BUTTON_MENU;
  172.           ActivateWindow(ewin);
  173.           break;
  174.         }
  175.         break;
  176.  
  177.      case MOUSEMOVE :
  178.         if (mouse_button & MOUSE_BUTTON_SELECT) {
  179.  
  180.            /* Save time for project title refresh delay while rubberbanding */
  181.            tp           = selected_template;
  182.            last_seconds = seconds;
  183.            last_micros  = micros;
  184.            seconds      = msg->Seconds;
  185.            micros       = msg->Micros;
  186.            switch (editor_mode) {
  187.               case EDITOR_MODE_CREATE :
  188.              if (mouse_button & MOUSE_BUTTON_RUBBER_BAND) {
  189.             draw_box(pwin, box);
  190.             box->bo_X2 = snap_x;
  191.             box->bo_Y2 = snap_y;
  192.             last_snap_x = snap_x;
  193.             last_snap_y = snap_y;
  194.             draw_box(pwin, box);
  195.             if (seconds != last_seconds || (micros -
  196.                    last_micros) > PROJECT_TITLE_REFRESH_DELAY) {
  197.                print_project_window_title();
  198.             }
  199.              }
  200.              break;
  201.  
  202.           case EDITOR_MODE_CLONE :
  203.              if (mouse_button & MOUSE_BUTTON_RUBBER_BAND) {
  204.             draw_box(pwin, box);
  205.             box->bo_X1 += snap_x - last_snap_x;
  206.             box->bo_Y1 += snap_y - last_snap_y;
  207.             box->bo_X2 += snap_x - last_snap_x;
  208.             box->bo_Y2 += snap_y - last_snap_y;
  209.             last_snap_x = snap_x;
  210.             last_snap_y = snap_y;
  211.             draw_box(pwin, box);
  212.             if (seconds != last_seconds || (micros -
  213.                    last_micros) > PROJECT_TITLE_REFRESH_DELAY) {
  214.                print_project_window_title();
  215.             }
  216.             tp->tp_Flags |= TEMPLATE_FLAG_MODIFIED;
  217.              }
  218.              break;
  219.  
  220.           case EDITOR_MODE_MODIFY :
  221.              if (mouse_button & MOUSE_BUTTON_RUBBER_BAND) {
  222.             draw_box(pwin, box);
  223.             if (modify_mode == MODIFY_MODE_MOVE) {
  224.                box->bo_X1 += snap_x - last_snap_x;
  225.                box->bo_Y1 += snap_y - last_snap_y;
  226.                box->bo_X2 += snap_x - last_snap_x;
  227.                box->bo_Y2 += snap_y - last_snap_y;
  228.             } else {
  229.                box->bo_X2 += snap_x - last_snap_x;
  230.                box->bo_Y2 += snap_y - last_snap_y;
  231.             }
  232.             last_snap_x = snap_x;
  233.             last_snap_y = snap_y;
  234.             draw_box(pwin, box);
  235.             if (seconds != last_seconds || (micros -
  236.                    last_micros) > PROJECT_TITLE_REFRESH_DELAY) {
  237.                print_project_window_title();
  238.             }
  239.             tp->tp_Flags |= TEMPLATE_FLAG_MODIFIED;
  240.              }
  241.              break;
  242.  
  243.           case EDITOR_MODE_DELETE :
  244.           case EDITOR_MODE_EDIT :
  245.              if (selected_template) {
  246.  
  247.             /* Check if mouse points to selected template */
  248.             if (find_template_by_pos(mouse_x, mouse_y) ==
  249.                          selected_template) {
  250.                if (!(mouse_button & MOUSE_BUTTON_RUBBER_BAND)) {
  251.                   mouse_button |= MOUSE_BUTTON_RUBBER_BAND;
  252.                   print_template_info();
  253.                   print_project_window_title();
  254.                }
  255.             } else {
  256.                if (mouse_button & MOUSE_BUTTON_RUBBER_BAND) {
  257.                   mouse_button &= ~MOUSE_BUTTON_RUBBER_BAND;
  258.                   clear_template_info();
  259.                   print_project_window_title();
  260.                }
  261.             }
  262.              }
  263.              break;
  264.            }
  265.         }
  266.         break;
  267.  
  268.      case NEWSIZE :
  269.         refresh_all_templates();
  270.         ActivateWindow(ewin);
  271.         template_list.tl_Flags |= TEMPLATE_LIST_FLAG_CHANGED;
  272.         break;
  273.       }
  274.       IReplyMsg(msg);
  275.    }
  276.    return(status);
  277. }
  278.     /* End rubber banding */
  279.  
  280.    STATIC VOID
  281. end_rubber_banding(VOID)
  282. {
  283.    struct Template    *tp;
  284.    struct Box         *box = ¤t_box;
  285.    struct BorderData  *bd;
  286.    struct TextData    *td;
  287.    struct GadgetData  *gd;
  288.    USHORT type, min_width, min_height, width = box->bo_X2 - box->bo_X1 + 1,
  289.           height = box->bo_Y2 - box->bo_Y1 + 1;
  290.  
  291.    /* First get current template type */
  292.    if (editor_mode == EDITOR_MODE_CREATE) {
  293.       type = template_type;
  294.    } else {
  295.       type = selected_template->tp_Type;
  296.    }
  297.  
  298.    /* Now calc minimal dimension required for template */
  299.    switch (type) {
  300.       case TEMPLATE_TYPE_SLIDER :
  301.       case TEMPLATE_TYPE_SCROLLER :
  302.       case TEMPLATE_TYPE_PALETTE :
  303.      if (width / 2 > height) {
  304.         min_width  = min_dimension[type].dim_Width;
  305.         min_height = min_dimension[type].dim_Height;
  306.      } else {
  307.         min_width  = min_dimension[type].dim_Height * 2;
  308.         min_height = min_dimension[type].dim_Width / 2;
  309.      }
  310.      break;
  311.  
  312.       default :
  313.      min_width  = min_dimension[type].dim_Width;
  314.      min_height = min_dimension[type].dim_Height;
  315.      break;
  316.    }
  317.  
  318.    /* Check template dimension is greater than minimal */
  319.    if (width < min_width || height < min_height) {
  320.       DisplayBeep((LONG)NULL);
  321.    } else {
  322.       switch (editor_mode) {
  323.      case EDITOR_MODE_CREATE :
  324.         tp = create_template();
  325.         display_template(tp);
  326.         break;
  327.  
  328.      case EDITOR_MODE_MODIFY :
  329.         tp = selected_template;
  330.         CopyMem((BYTE *)box, (BYTE *)
  331.                      &tp->tp_Box, (LONG)sizeof(struct Box));
  332.         /* Change template dimension */
  333.         switch (type) {
  334.            case TEMPLATE_TYPE_BORDER :
  335.           bd              = &tp->tp_Data.tp_BorderData;
  336.           bd->bd_LeftEdge = box->bo_X1;
  337.           bd->bd_TopEdge  = box->bo_Y1;
  338.           bd->bd_Width    = width;
  339.           bd->bd_Height   = height;
  340.           break;
  341.  
  342.            case TEMPLATE_TYPE_TEXT :
  343.           td              = &tp->tp_Data.tp_TextData;
  344.           td->td_LeftEdge = box->bo_X1;
  345.           td->td_TopEdge  = box->bo_Y1;
  346.  
  347.           /* Calc size of text for template box */
  348.           box        = &tp->tp_Box;
  349.           box->bo_X2 = box->bo_X1 + IPrintText(pri, pwin, td->td_Text,
  350.                    td->td_LeftEdge, td->td_TopEdge, td->td_Type,
  351.                   TEXT_DATA_FLAG_NO_PRINT, td->td_TextAttr) - 1;
  352.           box->bo_Y2 = box->bo_Y1 + td->td_TextAttr->ta_YSize - 1;
  353.           break;
  354.  
  355.            default :
  356.           gd              = &tp->tp_Data.tp_GadgetData;
  357.           gd->gd_LeftEdge = box->bo_X1;
  358.           gd->gd_TopEdge  = box->bo_Y1;
  359.           gd->gd_Width    = width;
  360.           gd->gd_Height   = height;
  361.           switch (gd->gd_Type) {
  362.              case GADGET_DATA_TYPE_SLIDER :
  363.             if (width / 2 < height) {
  364.                gd->gd_Flags |= GADGET_DATA_FLAG_ORIENTATION_VERT;
  365.             } else {
  366.                gd->gd_Flags &= ~GADGET_DATA_FLAG_ORIENTATION_VERT;
  367.             }
  368.             break;
  369.  
  370.              case GADGET_DATA_TYPE_SCROLLER :
  371.             if (width / 2 < height) {
  372.                gd->gd_Flags |= GADGET_DATA_FLAG_ORIENTATION_VERT;
  373.             } else {
  374.                gd->gd_Flags &= ~GADGET_DATA_FLAG_ORIENTATION_VERT;
  375.             }
  376.             break;
  377.  
  378.              case GADGET_DATA_TYPE_PALETTE :
  379.             if (width / 2 < height) {
  380.                gd->gd_Flags |= GADGET_DATA_FLAG_PALETTE_INDICATOR_TOP;
  381.             } else {
  382.                gd->gd_Flags &= ~GADGET_DATA_FLAG_PALETTE_INDICATOR_TOP;
  383.             }
  384.             gd->gd_SpecialData.gd_PaletteData.gd_PaletteDepth       = 2;
  385.             gd->gd_SpecialData.gd_PaletteData.gd_PaletteColorOffset = 0;
  386.             gd->gd_SpecialData.gd_PaletteData.gd_PaletteActiveColor = 2;
  387.             break;
  388.           }
  389.           break;
  390.         }
  391.         refresh_all_templates();
  392.         break;
  393.  
  394.      case EDITOR_MODE_CLONE :
  395.         tp = clone_template(selected_template, FALSE);
  396.         display_template(tp);
  397.         break;
  398.       }
  399.       template_list.tl_Flags |= TEMPLATE_LIST_FLAG_CHANGED;
  400.    }
  401. }
  402.     /* Print project window title */
  403.  
  404.    VOID
  405. print_project_window_title(VOID)
  406. {
  407.    struct Box  *box = ¤t_box;
  408.    BYTE   *title = &project_window_title[0];
  409.    USHORT width, height;
  410.  
  411.    if (box->bo_X2 > box->bo_X1) {
  412.       width = box->bo_X2 - box->bo_X1 + 1;
  413.    } else {
  414.       width = box->bo_X1 - box->bo_X2 + 1;
  415.    }
  416.    if (box->bo_Y2 > box->bo_Y1) {
  417.       height = box->bo_Y2 - box->bo_Y1 + 1;
  418.    } else {
  419.       height = box->bo_Y1 - box->bo_Y2 + 1;
  420.    }
  421.    switch (editor_mode) {
  422.       case EDITOR_MODE_CREATE :
  423.      if (mouse_button & MOUSE_BUTTON_RUBBER_BAND) {
  424.         SPrintf(title, " Left=%d Top=%d Width=%d Height=%d ", 
  425.                      box->bo_X1, box->bo_Y1, width, height);
  426.      } else {
  427.         SPrintf(title, " Create %s Template ",
  428.                    template_type_text_array[template_type]);
  429.      }
  430.      break;
  431.  
  432.       case EDITOR_MODE_MODIFY :
  433.      if (mouse_button & MOUSE_BUTTON_RUBBER_BAND) {
  434.         SPrintf(title, " Left=%d Top=%d Width=%d Height=%d ", 
  435.                      box->bo_X1, box->bo_Y1, width, height);
  436.      } else {
  437.         strcpy(title, " Modify Template ");
  438.      }
  439.      break;
  440.  
  441.       case EDITOR_MODE_DELETE :
  442.      if (mouse_button & MOUSE_BUTTON_RUBBER_BAND) {
  443.         SPrintf(title, " Left=%d Top=%d Width=%d Height=%d ", 
  444.                      box->bo_X1, box->bo_Y1, width, height);
  445.      } else {
  446.         strcpy(title, " Delete Template ");
  447.      }
  448.      break;
  449.  
  450.       case EDITOR_MODE_EDIT :
  451.      strcpy(title, " Edit Template ");
  452.      break;
  453.  
  454.       case EDITOR_MODE_USE :
  455.      strcpy(title, " Use Templates ");
  456.      break;
  457.    }
  458.    SetWindowTitles(pwin, title, (LONG)NULL);
  459. }
  460.     /* Start a new project */
  461.  
  462.    SHORT
  463. new_project(USHORT flags)
  464. {
  465.    struct NewWindow  *nwin = &project_new_window;
  466.    SHORT status;
  467.  
  468.    /* Free all templates */
  469.    free_template_list();
  470.    ISetGadgetAttributes(egl, EDITOR_GADGET_TEMPLATES, 0L, USE_CURRENT_VALUE,
  471.                  USE_CURRENT_VALUE, &template_list.tl_List);
  472.    clear_template_info();
  473.  
  474.    /* Close old project window and open new one */
  475.    if ((status = new_project_window(flags)) == EDITOR_STATUS_NORMAL) {
  476.       print_project_window_title();
  477.    }
  478.    show_error(status);
  479.    return(status);
  480. }
  481.     /* Close old project window and open new one */
  482.  
  483.    SHORT
  484. new_project_window(USHORT flags)
  485. {
  486.    struct NewWindow  *nwin = &project_new_window;
  487.    USHORT new_flags;
  488.    SHORT  status;
  489.  
  490.    /* Use default template list flags? */
  491.    if (flags & TEMPLATE_LIST_FLAG_DEFAULT_WINDOW) {
  492.       nwin->LeftEdge = 0;
  493.       nwin->TopEdge  = ewin->Height + 1;
  494.       nwin->Width    = wb_screen.Width;
  495.       nwin->Height   = wb_screen.Height - nwin->TopEdge;
  496.       flags          = template_list.tl_Flags = DEFAULT_TEMPLATE_LIST_FLAGS;
  497.    }
  498.  
  499.    /* Change render info */
  500.    if (flags & TEMPLATE_LIST_FLAG_BACK_FILL) {
  501.       new_flags = (PROJECT_RENDER_INFO_FLAGS | RENDER_INFO_FLAG_BACK_FILL);
  502.    } else {
  503.       new_flags = PROJECT_RENDER_INFO_FLAGS;
  504.    }
  505.    if (pri) {
  506.       IFreeRenderInfo(pri);
  507.    }
  508.    if (!(pri = IGetRenderInfo((struct Screen *)NULL, new_flags))) {
  509.       status = EDITOR_ERROR_OUT_OF_MEM;
  510.    } else {
  511.  
  512.       /* Change new window structure */
  513.       if (flags & TEMPLATE_LIST_FLAG_RESIZING) {
  514.      nwin->Flags |= WINDOWSIZING;
  515.       } else {
  516.      nwin->Flags &= ~WINDOWSIZING;
  517.       }
  518.       if (flags & TEMPLATE_LIST_FLAG_RENDER_COLORS) {
  519.      new_flags = (PROJECT_OPEN_WINDOW_FLAGS |
  520.                           OPEN_WINDOW_FLAG_RENDER_PENS);
  521.       } else {
  522.      nwin->DetailPen = PROJECT_WINDOW_DETAIL_PEN;
  523.      nwin->BlockPen  = PROJECT_WINDOW_BLOCK_PEN;
  524.      new_flags       = PROJECT_OPEN_WINDOW_FLAGS;
  525.       }
  526.  
  527.       /* Open new project window */
  528.       if (pwin) {
  529.      CloseWindow(pwin);
  530.       }
  531.       if (!(pwin = IOpenWindow(pri, nwin, new_flags))) {
  532.      status = EDITOR_ERROR_NO_WINDOW;
  533.       } else {
  534.      print_project_window_title();
  535.      status = EDITOR_STATUS_NORMAL;
  536.       }
  537.    }
  538.    show_error(status);
  539.    return(status);
  540. }
  541.     /* Clear project window */
  542.  
  543.    VOID
  544. clear_project_window(USHORT flags)
  545. {
  546.    SetRast(pwin->RPort, 0L);
  547.    RefreshWindowFrame(pwin);
  548.    if (flags & TEMPLATE_LIST_FLAG_BACK_FILL) {
  549.       IClearRenderWindow(pri, pwin, 0, 0, -1, -1);
  550.    }
  551. }
  552.